***************** Replication of Franchino, Fabio, and Camilla Mariotto. “Bargaining Outcomes and Success in EU Economic Governance Reforms”. Political Science Research and Methods.

* Step 1 to reproduce Figure A5

*************   EXPLAINING COUNTRY BARGAINING SUCCESS
*************   RE-CATEGORIZATION TEST SENSITIVITY ANALYSIS
clear all
version 16
pause on

use conflict_data1.dta, clear
*Two-level linear regression of success on x1...n with random intercepts issue_id, and robust standard errors
meglm c.success c.distSQ c.distCOM c.distPRES c.distEP i.ep_rule i.ep_rule#c.distEP c.extreme c.v_power c.salience_r c.pop c.gdp_pc if vot_right==2 || issue_id: , vce(robust)

* saving betas
matrix b = get(_b)
matrix list b
svmat b, names(b)
keep b*
drop if b1==.
save betas.dta, replace

set seed 1234

local v = 1
while `v' <= 1000 {

* upload original dataset
use conflict_data1.dta, clear
* drop position-related variables
drop success cntry_posAVE extreme distCOM distEP distSQ pos_pres_fin distPRES
sort country issue_id

******  RE-CATEGORIZATION
***** Select a random sample without replacement of observations
***** change 4 out of 35 obs by 25 points (half increases and half decreases)
*randomly select 4 issues for commission 
generate random = runiform()
by issue_id, sort: egen random_m = mean(random)
sort country random_m
gen selected_issue=issue_id in 1/4
scalar i1=selected_issue in 1
scalar i2=selected_issue in 2
scalar i3=selected_issue in 3
scalar i4=selected_issue in 4
display i1 " " i2 " " i3 " " i4
do meaningful_shift_commPROC.do
drop random random_m selected_issue

*randomly select 4 issues for parliament 
generate random = runiform()
by issue_id, sort: egen random_m = mean(random)
sort country random_m
gen selected_issue=issue_id in 1/4
scalar i1=selected_issue in 1
scalar i2=selected_issue in 2
scalar i3=selected_issue in 3
scalar i4=selected_issue in 4
display i1 " " i2 " " i3 " " i4
do meaningful_shift_ep_1PROC.do
drop random random_m selected_issue

*randomly change country positions
generate random = runiform()
sort random
do meaningful_shift_cntry_posPROC.do
drop random

****** DEPENDENT VARIABLE
gen success=-abs(cntry_pos-outcome) if vot_right==2
label variable success "Bargaining success =-abs(cntry_pos-outcome)"

****** INDEPENDENT VARIABLES
*****  Extremity of position
* compute mean country position
gen cntry_posAVE=.
label variable cntry_posAVE "Mean of positions of countries with voting rights"
local i = 1
while `i' <= 35 {
sum cntry_pos if issue_id== `i' & vot_right==2
replace cntry_posAVE=r(mean) if issue_id== `i' & vot_right==2
local i = `i' + 1
}
gen extreme=abs(cntry_pos-cntry_posAVE)
label variable extreme "Extremity of country position =abs(cntry_pos-cntry_posAVE)"

*****  Distance to Commission position
gen distCOM=abs(cntry_pos-comm) if vot_right==2
label variable distCOM "Distance to Commission position =abs(cntry_pos-comm)"

*****  Distance to Parliament position
gen distEP=abs(cntry_pos-ep_1) if vot_right==2
label variable distEP "Distance to EP position =abs(cntry_pos-ep_1)"

*****  Distance to SQ (sq is anchored at 0)
gen distSQ=cntry_pos if vot_right==2
label variable distSQ "Distance to SQ =cntry_pos"

*****  Finalizing Council president
* position of finalizing country president
gen pos_pres_fin=.
label variable pos_pres_fin "Position of finalizing presidency country"
local i = 1
while `i' <= 35 {
sum cntry_pos if issue_id== `i' & cou_pres_fin==2
replace pos_pres_fin=r(mean) if issue_id== `i'
local i = `i' + 1
}
* we set the positions of finalizing country presidents in these issues since they have no voting rights
replace pos_pres_fin=0 if issue_id==14 | issue_id==15 | issue_id==21
replace pos_pres_fin=50 if issue_id==12 | issue_id==28 | issue_id==29
replace pos_pres_fin=100 if issue_id==13 | issue_id==20 | issue_id==25 | issue_id==26 | issue_id==27 | issue_id==33 | issue_id==35
gen distPRES=abs(cntry_pos-pos_pres_fin) if vot_right==2
label variable distPRES "Distance to Fin Council Pres. position =abs(cntry_pos-pos_pres_fin)"

********* Two-level linear regression of success on x1...n with random intercepts issue_id, and robust standard errors
meglm c.success c.distSQ c.distCOM c.distPRES c.distEP i.ep_rule i.ep_rule#c.distEP c.extreme c.v_power c.salience_r c.pop c.gdp_pc || issue_id: , vce(robust)

********* Saving betas
matrix b = get(_b)
matrix list b
svmat b, names(b)
keep b*
drop if b1==.
save b.dta, replace
use betas.dta, clear
append using b.dta
save betas.dta, replace

local v = `v' + 1
}

rename b1	distSQ
rename b2   distCOM
rename b3	distPRES
rename b4	distEP
rename b5	ep_rule_1
rename b6	ep_rule_2
rename b7	ep_rule_distEP_1b
rename b8	ep_rule_distEP_2
rename b9	extreme
rename b10	v_power
rename b11	salience_r
rename b12	pop
rename b13	gdp_pc
rename b14	cons
rename b15	var_issue_id
rename b16	var_success

save betas.dta, replace